package com.spring.sunflower.common

import android.content.Context
import android.graphics.Color
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.spring.sunflower.App
import com.spring.sunflower.R
import com.spring.sunflower.base.BaseCompatActivity
import com.spring.sunflower.constant.Constant
import com.spring.sunflower.util.AppUtils
import com.spring.sunflower.util.ChannelInfoUtils
import com.spring.sunflower.util.DisplayUtils
import com.spring.sunflower.util.SPUtils
import kotlinx.android.synthetic.main.layout_tool_bar_dark.*
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView
import kotlin.collections.ArrayList

/**
 * Created by Kevin on 2021/7/17<br/>
 *
 * Describe:<br/>
 */
class WalletActivity : BaseCompatActivity() {
    private var magicIndicator: MagicIndicator? = null
    private var mViewPager: ViewPager? = null
    private var mAdapter: RelationAdapter? = null
    private var titleList = arrayListOf<String>("余额", "奖励", "收益")
    private var mFragments: MutableList<Fragment> = ArrayList()
    private var tvFunction: TextView? = null
    override fun initView() {
        tvTitle.text = "我的钱包"
        val intent = intent
        val pos = intent.getIntExtra(Constant.KEY_RELATION_TAB_POSITION, 0)
        tvFunction = findViewById(R.id.tvFunctionX)
        tvFunction!!.text = "账单"
        magicIndicator = findViewById(R.id.magicIndicator)
        mViewPager = findViewById(R.id.viewpager)
        mFragments.add(BalanceFragment())
        mFragments.add(AwardFragment())
        val channel = ChannelInfoUtils.getChannel(this)
        val qqChanel = channel.endsWith("tencent")
        if (AppUtils.isVerify(this) and qqChanel) {//应用宝渠道移除余额页面
            titleList.remove("收益")
        } else {
            mFragments.add(IncomeFragment())
        }
        initIndicator()
        mAdapter = RelationAdapter(supportFragmentManager, titleList, mFragments)
        mViewPager?.let {
            it.adapter = mAdapter
            it.setOffscreenPageLimit(2)
            it.setCurrentItem(pos)
        }
        tvFunction!!.setOnClickListener {
            startNewActivity(BillDetailActivity::class.java)
        }

    }

    private fun initIndicator() {
        val commonNavigator = CommonNavigator(this)
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return titleList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView =
                    ColorTransitionPagerTitleView(context)
                simplePagerTitleView.text = titleList[index]
                val channel = ChannelInfoUtils.getChannel(this@WalletActivity)
                val qqChannel = channel.endsWith("tencent")
                if (AppUtils.isVerify(this@WalletActivity) and qqChannel) {
                    simplePagerTitleView.width =
                        DisplayUtils.getScreenWidth(this@WalletActivity) / 2
                } else {
                    simplePagerTitleView.width =
                        DisplayUtils.getScreenWidth(this@WalletActivity) / 3
                }
                simplePagerTitleView.normalColor = Color.parseColor("#7f664630")
                simplePagerTitleView.selectedColor = Color.parseColor("#664630")
                simplePagerTitleView.setOnClickListener { mViewPager!!.currentItem = index }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 20.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.setColors(Color.parseColor("#664630"))
                return indicator
            }
        }
        magicIndicator!!.navigator = commonNavigator
        ViewPagerHelper.bind(magicIndicator, mViewPager)
    }

    override fun getLayoutID(): Int {
        return R.layout.activity_wallet
    }
}